package config

import (
	"gopkg.in/yaml.v3"
	"os"
	"path/filepath"
	"runtime"
)

type Config struct {
	Database  Database
	Jwt       Jwt
	Redis     Redis
	FileServe FileServe
	Rsa       Rsa
	Runtime   Runtime
}

type Database struct {
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	User     string `yaml:"user"`
	Password string `yaml:"password"`
	DB       string `yaml:"db"`
}

type Jwt struct {
	SecurityKey    string `yaml:"securityKey"`
	ExpirationDate int    `yaml:"expirationDate"`
	Encrypt        string `yaml:"encrypt"`
}

type Redis struct {
	Addr     string `yaml:"addr"`
	Password string `yaml:"password"`
	DB       int    `yaml:"db"`
}

type FileServe struct {
	Path        string `yaml:"path"`
	Domain      string `yaml:"domain"`
	SystemServe string `yaml:"systemServe"`
	PublicServe string `yaml:"publicServe"`
	Env         string `yaml:"env"`
}

type Rsa struct {
	Key string `yaml:"privateKey"`
}

type Runtime struct {
	ENV string `yaml:"env"`
}

var config Config

func GetConf() Config {
	_, file, _, _ := runtime.Caller(0)
	hostname, _ := os.Hostname()
	if hostname == "1panel" {
		file = filepath.Join(filepath.Dir(file), "prod.yml")
	} else {
		file = filepath.Join(filepath.Dir(file), "dev.yml")
	}
	yamlStr, err := os.ReadFile(file)
	if err != nil {
		panic(err)
	}
	// 将yaml文件内容映射到对应的struct 中
	err = yaml.Unmarshal(yamlStr, &config)
	if err != nil {
		return Config{}
	}
	return config
}
